<?php

namespace app\common\behavior;

use app\common\model\rate\Admin;
use app\common\model\rate\AdminCompany;
use app\common\model\rate\AuthGroupAccess;
use app\common\model\rate\CompanyRate;
use fast\Pinyin;
use fast\Random;
use think\Db;


/**
 * 生成管理员账号，并把账号加入对应权限组
 * 企业账号权限组：3，经销商权限组为4 ，平台管理员组为2，超级管理员组为1
 */
class AdminAuth
{
    /**
     * 加入权限组
     * type为账号类别
     * id为账号id
     */
    public function run(&$data) {
        //生成管理员账号
        $admin = self::createAdmin($data);

        $rateArr = $data['rate_c'];
       if ($data['is_has_spread'] == 0) {
           //把用户加入权限组
           (new AuthGroupAccess())->insert([
               'uid' => $admin['id'],
               'group_id' => config('site.compan_auth') //定死的
           ]);
           //特殊商户需要制定 管理员和企业的关系
            $rate_info = $rateArr[0];
            //非特殊商户
            $inner = [
                'code' => $data['code'],
                'company_id' => $data['company_id'],
                'rate_area_id' => $rate_info['rate_area_id'],
                'admin_id' => $admin['id']
            ];

            //记录企业和admin的关系
            $adminCompany = new AdminCompany();
            $adminCompany->insert($inner);
        } else {
           //特殊商户
           (new AuthGroupAccess())->insert([
               'uid' => $admin['id'],
               'group_id' => config('site.company_spread') //定死的
           ]);

           //特殊商户需要制定 管理员和企业的关系
           //非特殊商户
           $inner = [
               'code' => $data['code'],
               'company_id' => $data['company_id'],
               'admin_id' => $admin['id']
           ];

           //记录企业和admin的关系
           $adminCompany = new AdminCompany();
           $adminCompany->insert($inner);
       }

        $insert = [];
        foreach ($rateArr as $k => $v) {
            $temp['company_id'] = $data['company_id'];
            $temp['rate_area_id'] = $v['id'];
            $temp['createtime'] = time();
            $temp['evaluation_expression'] = $v['evaluation_expression'];
            $temp['year_tip'] = $v['year_tip'];
            $temp['month_tip'] = $v['month_tip'];
            $temp['limit_money'] = $v['limit_money'];
            $temp['is_check'] = $v['is_check'];

            array_push($insert, $temp);
        }

        $companyRate = new CompanyRate();
        $companyRate->insertAll($insert);
        //记录企业和admin的关系
        return true;
    }

    /**
     * 生成管理员账号
     * @param Object $company
     * name:企业名称
     * id: 企业id
     * type: 账号类型
     */
    private static function createAdmin($data) {
        $pinyin = Pinyin::get($data['name']);
        $admin['username'] = substr($pinyin, 0, 6).''.$data['company_id'];
        $admin['nickname'] = $data['name'];
        $admin['email'] = isset($data['email'])? $data['email'] : '';
        $admin['salt'] = Random::alnum();
        $admin['password'] = md5(md5(123456) . $admin['salt']);
        $admin['type'] = $data['is_has_spread'] == 0 ?2 : 4; //角色。
        $admin['who'] = $data['who']; //角色对应id
        $adminModel = new Admin();
        $res = $adminModel->validate('Admin.add')->data($admin)->isUpdate(false)->save();
        return $adminModel->toArray();
    }
}
